﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;


using Meqs.Entity;
using Meqs.Logic.Interface;
using log4net;

namespace Meqs.Logic.Implement
{
    public class ClassesDistributionSchoolLogic : CommonLogic<ClassesDistributionSchool, int>, IClassesDistributionSchoolLogic
    {
        /// <summary>
        /// 查村报表缓存状态
        /// </summary>
        /// <param name="ht">所需各种参数</param>
        /// <returns>返回0：缓存可用，1：缓存需要重新计算，－1：尚未定义报表参数</returns>
        public int CheckCatch(Hashtable ht)
        {
            //参数需要班级id，考试id，学校编码
            //判断缓存可用性。不可用则重新计算

            try
            {
                //1.获取各参数项
                string statementName = "DistributionSettingsSchool.Find";
                Hashtable table = new Hashtable();
                table.Add("ExamID", ht["ExamID"]);
                table.Add("SchoolMark", ht["SchoolMark"]);
                IList<DistributionSettingsSchool> kes = ExecuteQueryForList<DistributionSettingsSchool>(statementName, table);

                foreach (DistributionSettingsSchool k in kes)
                {
                    table = new Hashtable();
                    table.Add("ExamID", ht["ExamID"]);
                    table.Add("ItemID", k.RID);
                    table.Add("ClassID", ht["ClassID"]);
                    table.Add("SchoolMark", ht["SchoolMark"]);
                    IList<ClassesDistributionSchool> cke = FindAll(table);
                    //2 判断缓存可用性
                    if (cke.Count != 1 || cke[0].Dirty == 1)
                    {
                        //3 不可用删除，重新计算保存
                        ExecuteDelete("ClassesDistributionSchool.DeleteCatch", table);
                        ReSetCatch(table, k);
                    }
                }
                if (kes.Count == 0)
                    return -1;

            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("更新对象出错：" + ex.Message, ex);
                throw;
            }

            return 0;
        }
        /// <summary>
        /// 计算得到第二次得操作符
        /// </summary>
        /// <param name="middleop"></param>
        /// <returns></returns>
        private int GetSecongOp(int middleop)
        {
            switch (middleop)
            {
                case 2:
                    middleop = 1;
                    break;
                case 1:
                    middleop = 2;
                    break;
                case -1:
                    middleop = -2;
                    break;
                case -2:
                    middleop = -1;
                    break;
            }
            return middleop;
        }
        /// <summary>
        /// 重新计算缓存报表数据
        /// </summary>
        /// <param name="ht">所需要的各类参数</param>
        public void ReSetCatch(Hashtable ht, DistributionSettingsSchool rebuildsetting)
        {
            try
            {
                //1.计算正方向
                string statementName = "DistributionSettingsSchool.FindByOrder";
                Hashtable table = new Hashtable();
                table.Add("ExamID", ht["ExamID"]);
                table.Add("Operator", ">");
                table.Add("SchoolMark", ht["SchoolMark"]);
                table.Add("SubjectID", rebuildsetting.SubjectID);
                table.Add("Forwhat", "operator desc,num asc");
                IList<DistributionSettingsSchool> kes = ExecuteQueryForList<DistributionSettingsSchool>(statementName, table);
                double middle = -1;
                int middleOp = -3;
                //计算命中和累计
                foreach (DistributionSettingsSchool k in kes)
                {
                    table = new Hashtable();
                    table.Add("Type", 1);
                    if (k.SubjectID == 0)
                    {
                        table.Add("Condition", " ");
                    }
                    else
                    {
                        table.Add("Condition", " and subjectid = " + k.SubjectID);
                    }
                    table.Add("TableName", ht["SchoolMark"].ToString() + ".ExamDetail");
                    table.Add("ExamID", ht["ExamID"]);
                    table.Add("Number", k.Num);
                    table.Add("SchoolMark", "''");
                    double stuscore = ExecuteQueryForObject<double>("GradeScoreEducation.GetStuPositionScore", table);

                    if (Convert.ToInt32(ht["ItemID"]) == k.RID)
                    {
                        string forwhat = "Score " + ReportCatchBuild.GetTypeString(k.Operator) + stuscore;

                        table = new Hashtable();
                        table.Add("ExamID", ht["ExamID"]);
                        table.Add("SubjectID", k.SubjectID);
                        table.Add("SchoolMark", ht["SchoolMark"]);
                        table.Add("ClsID", ht["ClassID"]);
                        table.Add("ForWhat", forwhat);
                        //累计
                        int tempsum = -1;
                        if (k.SubjectID == 0)
                        {
                            tempsum = ExecuteQueryForObject<int>("ExamDetailSchool.FindFullCount", table);
                        }
                        else
                        {
                            tempsum = ExecuteQueryForObject<int>("ExamDetailSchool.FindCount", table);
                        }


                        int tempin = -1;
                        if (middle == -1)
                        {
                            //第一次命中于累计相同
                            tempin = tempsum;
                        }
                        else
                        {
                            //计算上次保存的区间上届
                            forwhat += " and Score " + ReportCatchBuild.GetTypeString(GetSecongOp(middleOp) * -1) + middle;
                            table.Remove("ForWhat");
                            table.Add("ForWhat", forwhat);

                            if (k.SubjectID == 0)
                            {
                                tempin = ExecuteQueryForObject<int>("ExamDetailSchool.FindFullCount", table);
                            }
                            else
                            {
                                tempin = ExecuteQueryForObject<int>("ExamDetailSchool.FindCount", table);
                            }
                        }


                        ClassesDistributionSchool g = new ClassesDistributionSchool();
                        g.ClassID = Convert.ToInt32(ht["ClassID"]);
                        g.SchoolTestID = Convert.ToInt32(ht["ExamID"]);
                        g.ItemID = Convert.ToInt32(ht["ItemID"]);
                        g.Dirty = 0;
                        g.InNum = tempin;
                        g.OverNum = tempsum;
                        g.SchoolMark = ht["SchoolMark"].ToString();
                        Create(g);
                        break;
                    }
                    middle = stuscore;
                    middleOp = k.Operator;


                }


                //2 计算负方向
                middle = -1;
                table = new Hashtable();
                table.Add("ExamID", ht["ExamID"]);
                table.Add("Operator", "<");
                table.Add("SchoolMark", ht["SchoolMark"]);
                table.Add("SubjectID", rebuildsetting.SubjectID);
                table.Add("Forwhat", "operator asc,num asc");
                kes = ExecuteQueryForList<DistributionSettingsSchool>(statementName, table);
                //计算命中和累计
                foreach (DistributionSettingsSchool k in kes)
                {
                    table = new Hashtable();
                    table.Add("Type", 0);
                    if (k.SubjectID == 0)
                    {
                        table.Add("Condition", " ");
                    }
                    else
                    {
                        table.Add("Condition", " and subjectid = " + k.SubjectID);
                    }
                    table.Add("TableName", ht["SchoolMark"].ToString() + ".ExamDetail");
                    table.Add("ExamID", ht["ExamID"]);
                    table.Add("Number", k.Num);
                    table.Add("SchoolMark", "''");
                    double stuscore = ExecuteQueryForObject<double>("GradeScoreEducation.GetStuPositionScore", table);

                    if (Convert.ToInt32(ht["ItemID"]) == k.RID)
                    {
                        string forwhat = "Score " + ReportCatchBuild.GetTypeString(k.Operator) + stuscore;

                        table = new Hashtable();
                        table.Add("ExamID", ht["ExamID"]);
                        table.Add("SubjectID", k.SubjectID);
                        table.Add("SchoolMark", ht["SchoolMark"]);
                        table.Add("ClsID", ht["ClassID"]);
                        table.Add("ForWhat", forwhat);
                        //累计
                        int tempsum = -1;
                        if (k.SubjectID == 0)
                        {
                            tempsum = ExecuteQueryForObject<int>("ExamDetailSchool.FindFullCount", table);
                        }
                        else
                        {
                            tempsum = ExecuteQueryForObject<int>("ExamDetailSchool.FindCount", table);
                        }

                        int tempin = -1;
                        if (middle == -1)
                        {
                            //第一次命中于累计相同
                            tempin = tempsum;
                        }
                        else
                        {
                            //计算上次保存的区间上界
                            forwhat += " and Score " + ReportCatchBuild.GetTypeString(GetSecongOp(middleOp) * -1) + middle;
                            table.Remove("ForWhat");
                            table.Add("ForWhat", forwhat);
                            if (k.SubjectID == 0)
                            {
                                tempin = ExecuteQueryForObject<int>("ExamDetailSchool.FindFullCount", table);
                            }
                            else
                            {
                                tempin = ExecuteQueryForObject<int>("ExamDetailSchool.FindCount", table);
                            }


                        }


                        ClassesDistributionSchool g = new ClassesDistributionSchool();
                        g.ClassID = Convert.ToInt32(ht["ClassID"]);
                        g.SchoolTestID = Convert.ToInt32(ht["ExamID"]);
                        g.ItemID = Convert.ToInt32(ht["ItemID"]);
                        g.Dirty = 0;
                        g.InNum = tempin;
                        g.OverNum = tempsum;
                        g.SchoolMark = ht["SchoolMark"].ToString();
                        Create(g);
                        break;
                    }
                    middle = stuscore;
                    middleOp = k.Operator;


                }

            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("更新对象出错：" + ex.Message, ex);
                throw;
            }
        }
        #region Implementation of ICommonLogic<ClassesDistributionSchool, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(ClassesDistributionSchool entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(ClassesDistributionSchool entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public ClassesDistributionSchool FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ClassesDistributionSchool> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ClassesDistributionSchool> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion
    }
}